package com.zzptc.zzsgz.bdsjws.activities;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.SelectContactFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllContactFragment;
import com.zzptc.zzsgz.bdsjws.vo.Contact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddContactsActivity extends BaseActivity {
    ShowAllContactFragment showAllContactFragment;//第一个碎片
    SelectContactFragment selectContactFragment;
    @BindView(R.id.et_contact)
    EditText etContact;

    boolean flag = true;
    List<Contact> list;
    List<Contact> selectlist = new ArrayList<Contact>();
    @BindView(R.id.tv_count)
    TextView tvCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_add_contacts;
    }

    @Override
    public void initData() {
        tvCount.setText("(" + MyApplication.goSosList().size() + ")");

        list = MyApplication.getList();//获得所有联系人
        if (showAllContactFragment == null) {
            showAllContactFragment = new ShowAllContactFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.fl_show_contact, showAllContactFragment).addToBackStack("contact").commit();
    }

    /**
     * 处理回退异常
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }
    }

    @Override
    public void initView() {
        //监听事件
        etContact.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //Toast.makeText(AddContactsActivity.this, "===", Toast.LENGTH_SHORT).show();
                if (flag) {
                    if (selectContactFragment == null) {
                        selectContactFragment = new SelectContactFragment();
                    }
                    //替换碎片Fragment
                    getFragmentManager().beginTransaction().hide(showAllContactFragment).add(R.id.fl_show_contact, selectContactFragment).addToBackStack("contact").commit();
                    flag = false;
                }
            }

            @Override
            public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
                final CharSequence c = charSequence.toString().replace(" ", "").replace("-", "");
                Toast.makeText(AddContactsActivity.this, charSequence, Toast.LENGTH_SHORT).show();
                selectlist.clear();
                //项  适配器  都可以共用
                //耗时操作放入线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int j = 0; j < list.size(); j++) {
                            Contact contact = list.get(j);
                            if (contact.getName().contains(c) || contact.getPhonenum().toString().replace(" ", "").replace("-", "").contains(c)) {
                                //产生一个新的集合  true 匹配上了  false没有匹配的电话号码或联系人姓名
                                selectlist.add(contact);
                            }

                        }

                        //循环完毕之后 更新第二个碎片
                        // selectContactFragment.getSelectContactList(selectlist);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectContactFragment.getSelectContactList(selectlist);
                            }
                        });
                    }
                }).start();


            }

            @Override
            public void afterTextChanged(Editable editable) {
                //当删除完毕   要不就回退到前页    要不就显示所有的联系人
                if (etContact.getText().length() == 0) {
                    if (getFragmentManager().getBackStackEntryCount() > 1) {
                        getFragmentManager().popBackStack();
                    }
                    flag = true;//开关再次打开
                }

            }
        });
    }


    @OnClick(R.id.tv_sos_tel_quedin)
    public void onViewClicked() {


    }
}
